//
// Created by THE13 on 2024/2/14.
//

#ifndef HUB_USB_ST7789_H
#define HUB_USB_ST7789_H



/*-------------------------------------------------------------------- c source --------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "st7789_config_io.h"
#include "st7789_config_view.h"
#include "../driver_interface.h"

/* Control Registers and constant codes */
#define ST7789_NOP     0x00
#define ST7789_SWRESET 0x01
#define ST7789_RDDID   0x04
#define ST7789_RDDST   0x09

#define ST7789_SLPIN   0x10
#define ST7789_SLPOUT  0x11
#define ST7789_PTLON   0x12
#define ST7789_NORON   0x13

#define ST7789_INVOFF  0x20
#define ST7789_INVON   0x21
#define ST7789_DISPOFF 0x28
#define ST7789_DISPON  0x29
#define ST7789_CASET   0x2A
#define ST7789_RASET   0x2B
#define ST7789_RAMWR   0x2C
#define ST7789_RAMRD   0x2E

#define ST7789_PTLAR   0x30
#define ST7789_COLMOD  0x3A
#define ST7789_MADCTL  0x36

#define ST7789_TEARING_EFFECT     0x35
#define ST7789_PORCH_CTRL         0xB2
#define ST7789_GATE_CTRL          0xB7
#define ST7789_VCOM_SET           0xBB
#define ST7789_DISPLAY_OFF        0xBD
#define ST7789_LCM_CTRL           0xC0
#define ST7789_VDV_VRH_EN         0xC2
#define ST7789_VDV_SET            0xC4
#define ST7789_VCOMH_OFFSET_SET   0xC5
#define ST7789_FR_CTRL            0xC6
#define ST7789_POWER_CTRL         0xD0
#define ST7789_PV_GAMMA_CTRL      0xE0
#define ST7789_NV_GAMMA_CTRL      0xE1

/**
 * Memory Data Access Control Register (0x36H)
 * MAP:     D7  D6  D5  D4  D3  D2  D1  D0
 * param:   MY  MX  MV  ML  RGB MH  -   -
 *
 */

/* Page Address Order ('0': Top to Bottom, '1': the opposite) */
#define ST7789_MADCTL_MY  0x80
/* Column Address Order ('0': Left to Right, '1': the opposite) */
#define ST7789_MADCTL_MX  0x40
/* Page/Column Order ('0' = Normal Mode, '1' = Reverse Mode) */
#define ST7789_MADCTL_MV  0x20
/* Line Address Order ('0' = LCD Refresh Top to Bottom, '1' = the opposite) */
#define ST7789_MADCTL_ML  0x10
/* RGB/BGR Order ('0' = RGB, '1' = BGR) */
#define ST7789_MADCTL_RGB 0x00

#define ST7789_RDID1   0xDA
#define ST7789_RDID2   0xDB
#define ST7789_RDID3   0xDC
#define ST7789_RDID4   0xDD

/* Advanced options */
#define ST7789_COLOR_MODE_16bit 0x65    //  RGB565 (16bit) RGB565-262K->0x65  RGB565-65K->0x55 //65K色的屏幕较昏暗，262K色的较艳丽
#define ST7789_COLOR_MODE_18bit 0x66    //  RGB666 (18bit)


//0. init the st7789 conctrler
void ST7789_Init(void);
void ST7789_WriteData_Step1(uint8_t *buff);
void ST7789_WriteData_Step2(uint8_t *buff);

void ST7789_WriteCommand(uint8_t cmd);

//1. st7789 write data
void ST7789_WriteData(uint8_t *buff, size_t buff_size);
//2. Set the rotation direction of the display
void ST7789_SetRotation(uint8_t m);
//3. Fill the DisplayWindow with single color
void ST7789_Fill_Color(uint16_t color);
//4.Set address of DisplayWindow
void ST7789_SetAddressWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);

extern uint8_t st7789_init_flag;



#endif //HUB_USB_ST7789_H
